﻿using System.ComponentModel;
using System.Windows.Forms;
using mvLibrary.Helpers.Utils;
using mvLibrary.Helpers.Interfaces;

namespace mvLibrary.controls
{
    public partial class mvMenuItem : ToolStripMenuItem
    {
        public mvMenuItem()
        {
            InitializeComponent();
        }

        [Category("Multivalue")]
        public string SecurityName { get; set; }
        [Category("Multivalue")]
        public string FullAccessName { get; set; }

        public string UserId { get; set; }


        protected override void OnMouseUp(MouseEventArgs e)
        {

            IMasterForm masterForm = this.GetMasterForm(this);
            bool esUsuarioPrincipal = FenkUtils.EsUsuarioPrincipal(masterForm.UserId);

            if (e.Button.Equals(MouseButtons.Right) && !this.HasChildrens() && esUsuarioPrincipal)
            {
                MessageBoxCreator.SecurityMessage(this.Text, this.SecurityName);


                masterForm.AbrirFormularioPermisosEspecialesDesdeAfuera(this.SecurityName);
            }
            else if (e.Button.Equals(MouseButtons.Right) && this.OwnerItem == null && esUsuarioPrincipal)
            {
                MessageBoxCreator.FullAccessSecurityMessage(this.Text,this.FullAccessName);

                masterForm.AbrirFormularioPermisosEspecialesDesdeAfuera(this.FullAccessName);
            }
        }
        
        public bool HasChildrens()
        {
            return this.DropDown.Items.Count > 0;
        }

        public ToolStripItemCollection Childrens()
        {
            return this.DropDown.Items;
        }
        
        internal bool HasParent()
        {
            return this.Parent != null;
        }

        private IMasterForm GetMasterForm(mvMenuItem toolStripItem)
        {
            IMasterForm masterForm = null;

            if (toolStripItem.OwnerItem != null)
            {
               masterForm = this.GetMasterForm(toolStripItem.OwnerItem as mvMenuItem);
            }
            else
            {
                masterForm = toolStripItem.Parent.Parent as IMasterForm;
            }

            return masterForm;
        }
    }
}
